/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomOptionsActionsTable.java
 *
 * Created on 10-mars-2009, 20:26:10
 */

package View.ClusterizationPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.*;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Group;
/**
 *
 * @author vf
 */
public class CustomOptionsActionsWindow extends javax.swing.JFrame {

    CustomOptionsPanel main;

    /** Creates new form CustomOptionsActionsTable */
    public CustomOptionsActionsWindow(CustomOptionsPanel main) {
        this.main=main;
        initComponents();
        setTitle("Clusters assignations: "+main.getName());
        setSize(500, 400);
        //setLocation(main.getLocationOnScreen().x+main.getWidth(),
        //            main.getLocationOnScreen().y);
        addWindowListener(
            new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    hideWin();
                }
            }
        );
    }

    public void updateName(){
        setTitle("Clusters assignations: "+main.getName());
    }

    public String getAttributeName(int i) {
        return main.getAttributeName(i);
    }

    public int getAttributeNbr() {
        return main.getAttributeNbr();
    }

    public Color getClusterColor(int i) {
        return main.getClusterColor(i);
    }

    public String getClusterName(int i) {
        return main.getClusterName(i);
    }

    public int getNumberOfActions() {
        return main.getNumberOfActions();
    }

    public Object getValue(int actionIndex, int clusterIndex) {
        return main.getValue(actionIndex, clusterIndex);
    }

    public int getActionsCountAssignedTo(int clusterIndex){
        return actionsTable.getActionsCountAssignedTo(clusterIndex);
    }

    public int getActionAssignedTo(int clusterIndex,int actionIndex){
        return actionsTable.getActionAssignedTo(clusterIndex, actionIndex);
    }

    public void updateActionSelection(int[] selectedRows) {
        main.updateActionSelection(selectedRows);
    }

    private void addSelected() {
        int clusterIndex = main.getSelectedCluster();
        int[] actions = actionsTable.getSelectedRows();
        if (clusterIndex>=0){//-1 si aucun cluster n'est selectionné
            for (int i=0;i<actions.length;i++)
                actionsTable.assignCluster(actions[i], clusterIndex);
        }
        main.repaintClusterTable();
    }

    private void clearSelected() {
        int[] actions = actionsTable.getSelectedRows();
        for (int i=0;i<actions.length;i++)
            actionsTable.clearAssignedCluster(actions[i]);
        main.repaintClusterTable();
    }

    public void clearAllActionsAssignedTo(int clusterIndex){
        actionsTable.clearAllActionsAssignedTo(clusterIndex);
    }
    private void hideWin(){
        setVisible(false);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {

        buttonAdd = new javax.swing.JButton();
        buttonClose = new javax.swing.JButton();
        buttonClear = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        actionsTable = new CustomOptionsActionsTable();

        actionsTable.setParent(this);

        buttonAdd.setText("Add to selected cluster");
        buttonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addSelected();
            }
        });

        buttonClose.setText("Close");
        buttonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hideWin();
            }
        });

        buttonClear.setText("Clear");
        buttonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearSelected();
            }
        });

        jScrollPane1.setViewportView(actionsTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(buttonAdd)
                .addGap(26, 26, 26)
                .addComponent(buttonClear)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                .addComponent(buttonClose)
                .addGap(35, 35, 35))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonAdd)
                    .addComponent(buttonClear)
                    .addComponent(buttonClose))
                .addContainerGap())
        );

        pack();
    }

    // Variables declaration - do not modify
    private CustomOptionsActionsTable actionsTable;
    private javax.swing.JButton buttonAdd;
    private javax.swing.JButton buttonClear;
    private javax.swing.JButton buttonClose;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration

}
